package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.InsureModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.model.enty.insure.InsureData;
import com.tianhui.consignortwofei.mvp.model.enty.insure.InsureInfo;
import com.tianhui.consignortwofei.mvp.model.enty.insure.InsureQueryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.insure.InsureResultDataInfo;
import com.tianhui.consignortwofei.mvp.model.enty.insure.InsureResultInfo;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2020/9/24
 * @description
 **/
public class InsureDetailActivity extends BaseActivity {
    @BindView(R.id.activity_insure_detail_insureAmountTextView)
    TextView mInsureAmountTextView;
    @BindView(R.id.activity_insure_detail_insurePriceTextView)
    TextView mInsurePriceTextView;
    @BindView(R.id.activity_insure_detail_insureNameTextView)
    TextView mInsureNameTextView;
    @BindView(R.id.activity_insure_detail_startDateTextView)
    TextView mStartDateTextView;
    @BindView(R.id.activity_insure_detail_endDateTextView)
    TextView mEndDateTextView;


    private InsureModel mInsureModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_insure_detail;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "保单明细";
    }

    @Override
    public void init() {


        mInsureModel = new InsureModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            OrderInfo orderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (orderInfo != null) {
                requestInsureQuery(orderInfo);
            }
        }

    }

    private void requestInsureQuery(OrderInfo orderInfo) {
        Map<String, String> map = new HashMap<>();
        map.put("dispatchno", orderInfo.dispatchno);
        mInsureModel.insureQueryModel(this, true, map, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<InsureQueryInfo>>() {
            @Override
            public void onNext(CommonResponse<InsureQueryInfo> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    InsureQueryInfo insureQueryInfo = commonResponse.getData();
                    if (insureQueryInfo != null) {
                        requestInsureInfo(insureQueryInfo);
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

            }
        });
    }

    private void requestInsureInfo(InsureQueryInfo insureQueryInfo) {
        mInsureModel.insureInfoModel(this, true, insureQueryInfo, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<InsureData>>() {
            @Override
            public void onNext(CommonResponse<InsureData> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    InsureData insureData = commonResponse.getData();
                    if (insureData != null && !TextUtils.isEmpty(insureData.status) && insureData.status.equals("1001")) {
                        InsureResultInfo insureResultInfo = insureData.result;
                        if (insureResultInfo != null) {
                            InsureResultDataInfo resultDataInfo = insureResultInfo.data;
                            if (resultDataInfo != null) {
                                mInsureAmountTextView.setText(resultDataInfo.amount);
                                mInsurePriceTextView.setText("保费：" + resultDataInfo.payPrem);
                                mStartDateTextView.setText("起始日期：" + resultDataInfo.startDate);
                                mEndDateTextView.setText("截止日期：" + resultDataInfo.endDate);
                                InsureInfo insureInfo = resultDataInfo.applicantInfo;
                                if (insureInfo != null) {
                                    mInsureNameTextView.setText("投保人：" + insureInfo.name);
                                }
                            }
                        }
                    }


                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }
}
